package com.example.joke.base.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

public class AlertController {

    private AlertDialog mDialog;
    private Window mWindow;

    private DialogViewHelper mViewHelper;

    public AlertController(AlertDialog alertDialog, Window window) {
        this.mDialog = alertDialog;
        this.mWindow = window;
    }

    public void setViewHelper(DialogViewHelper mViewHelper) {
        this.mViewHelper = mViewHelper;
    }

    public void setText(int viewId, CharSequence text) {
        mViewHelper.setText(viewId, text);
    }

    public  <T extends View> T getView(int viewId) {
        return mViewHelper.getView(viewId);
    }

    // 设置点击事件
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        mViewHelper.setOnClickListener(viewId, listener);
    }

    public AlertDialog getDialog() {
        return mDialog;
    }

    public Window getWindow() {
        return mWindow;
    }

    public static class AlertParams {

        public Context mContext;
        public int mThemeResId;
        // 点击空白是否能够取消, 默认点击阴影能够取消
        public boolean mCancelable = true;
        // dialog Cancel监听
        public DialogInterface.OnCancelListener mOnCancelListener;
        // dialog Dismiss监听
        public DialogInterface.OnDismissListener mOnDismissListener;
        // dialog key监听
        public DialogInterface.OnKeyListener mOnKeyListener;
        // 布局的view
        public View mView;
        // 布局的layout id
        public int mViewLayoutResId;

        // 按钮字体的修改
        public SparseArray<CharSequence> mTextArray = new SparseArray<>();
//        public SparseArray<WeakReference<View.OnClickListener>> mClickArray = new SparseArray<>();
        public SparseArray<View.OnClickListener> mClickArray = new SparseArray<>();
        // 宽度
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 高度
        public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;

        // 动画
        public int mAnimation = 0;
        // dialog显示的位置
        public int mGravity = Gravity.CENTER;

        public AlertParams(Context context, int themeResId) {
            this.mContext = context;
            this.mThemeResId = themeResId;
        }

        // 绑定和设置参数
        public void apply(AlertController mAlert) {
            // 1. 设置dialog布局 DialogViewHelper
            DialogViewHelper viewHelper = null;
            if (mViewLayoutResId != 0) {
                viewHelper = new DialogViewHelper(mContext, mViewLayoutResId);
            }

            if (mView != null) {
                viewHelper = new DialogViewHelper();
                viewHelper.setContentView(mView);
            }

            if (viewHelper == null) {
                throw new IllegalArgumentException("请设置布局setContentView()");
            }

            // 给dialog设置布局
            mAlert.getDialog().setContentView(viewHelper.getContentView());

            // 设置controller的辅助类
            mAlert.setViewHelper(viewHelper);

            // 2. 设置文本，遍历集合
            int textArraySize = mTextArray.size();
            for (int i = 0; i < textArraySize; i++) {
                mAlert.setText(mTextArray.keyAt(i), mTextArray.valueAt(i));
            }

            // 3. 设置点击
            int clickArraySize = mClickArray.size();
            for (int i = 0; i < clickArraySize; i++) {
                mAlert.setOnClickListener(mClickArray.keyAt(i), mClickArray.valueAt(i));
            }


            // 4. 配置自定义的效果，全屏，从底部弹出，默认动画
            Window window = mAlert.getWindow();

            // 设置位置
            window.setGravity(mGravity);

            // 设置动画
            if (mAnimation != 0) {
                window.setWindowAnimations(mAnimation);
            }

            // 设置宽高
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = mWidth;
            params.height = mHeight;
            window.setAttributes(params);
        }
    }

}
